<template>
  <el-dialog :visible="showDialog" title="新增员工" @close="closeFn">
    <el-form ref="formAdd" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="formData.username"
          style="width: 50%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="formData.mobile"
          style="width: 50%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="formData.timeOfEntry"
          placeholder="请选入职择日期"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="formData.formOfEmployment"
          style="width: 50%"
          placeholder="请选择聘用形式"
        >
          <!-- label 为下拉框显示内容  value为选中的值-->
          <el-option v-for="obj in hireType" :key="obj.id" :value="obj.id" :label="obj.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="formData.workNumber"
          style="width: 50%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input
          v-model="formData.departmentName"
          style="width: 50%"
          placeholder="请输入部门"
          @focus="chooseDepartment"
        />
      </el-form-item>

      <!-- 部门树形结构 -->
      <el-form-item v-show="showTree" v-loading="loading">
        <el-tree :data="treeList" :props="{label: 'name'}" :default-expand-all="true" @node-click="treeNode" />
      </el-form-item>

      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          v-model="formData.correctionTime"
          placeholder="请选择转正日期"
        />
      </el-form-item>
    </el-form>
    <!-- slot="footer" 向下显示 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-button type="primary" @click="determineFn">确定</el-button>
      <el-button @click="closeFn">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { validMobile } from '@/utils/validate'
import { addStaffApi, getTissueListApi } from '@/api'
import Employees from '@/api/constant/employees'
import { getDepartments } from '@/utils'
export default {
  name: 'VueAdminTemplateMasterAddEmployees',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const checkMobilePhone = (rule, value, callback) => {
      validMobile(value) ? callback() : callback(new Error('手机号格式不正确'))
    }
    return {
      formData: {
        username: '', // 姓名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 组织名称
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        formOfEmployment: [
          { required: true, trigger: 'blur', message: '请输入聘用形式' }
        ],
        username: [
          { required: true, trigger: 'blur', message: '请输入姓名' },
          {
            max: 4,
            min: 1,
            message: '姓名为1~4位',
            trigger: 'blur'
          }
        ],
        mobile: [
          { required: true, trigger: 'blur', message: '请输入手机号' },
          { validator: checkMobilePhone, trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, trigger: 'blur', message: '请输入入职时间' }
        ],
        workNumber: [
          { required: true, trigger: 'blur', message: '请输入工号' }
        ],
        departmentName: [
          { required: true, trigger: 'change', message: '请输入部门' }
        ],
        correctionTime: [
          { required: true, trigger: 'blur', message: '请输入转正时间' }
        ]
      },
      hireType: Employees.hireType, // 下拉框数据
      treeList: [], // 树形结构数组
      showTree: false, // 树形结构状态
      loading: false // 树形结构加载状态
    }
  },

  mounted() {},

  methods: {
    // 关闭窗口后的回调
    closeFn() {
      // 清除表单数据
      this.formData = {}
      // 清除校验结果
      this.$refs.formAdd.resetFields()
      // 通知父组件改变弹出窗状态
      this.$emit('update:showDialog', false)
    },

    // 部门 -> 聚焦 -> 触发
    async chooseDepartment() {
      this.showTree = true
      this.loading = true
      const { depts } = await getTissueListApi()
      this.treeList = getDepartments(depts, '')
      this.loading = false
    },

    //  树形节点 -> 点击 -> 触发  接收3个参数
    treeNode(node) {
      this.formData.departmentName = node.introduce
      this.showTree = false
    },

    // 确定 -> 校验表单
    async determineFn() {
    //   let data
    //   // 判断 下拉框值是否为空
    //   if (this.formData.formOfEmployment) {
    //     // 替换 聘用方式为枚举中的 数字id
    //     this.formData.formOfEmployment = this.hireType.find(
    //       (item) => item.value === this.formData.formOfEmployment
    //     ).id
    //     data = this.formData
    //   }
      try {
        await this.$refs.formAdd.validate()
        await addStaffApi(this.formData)
        this.$message.success('增加成功！')
        // 判断父组件是否有此方法
        this.$parent.getHrTable() && this.$parent.getHrTable()
        this.$emit('update:showDialog', false)
      } catch (error) {
        this.$message.error('表单校验不通过!')
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
